/**
 * 
 */
package xj.toolkit.ebus.demo;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import xj.toolkit.ebus.Emitter;
import xj.toolkit.ebus.EventBus;
import xj.toolkit.ebus.SimpleBus;


/**
 * @author isdom
 *
 */
public class Demo {

    @SuppressWarnings("unused")
    private void func1(String str) {
        System.out.println("["+ Thread.currentThread().getId() +"] in func1:" + str);
    }
    
    @SuppressWarnings("unused")
    private void func2(Emitter emitter) {
        System.out.println("["+ Thread.currentThread().getId() +"] in func2");
        emitter.emit("Hello, world.");
    }
    
    private static Runnable unreg = null;
    
    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        final EventBus    ebus = new SimpleBus();
        final ScheduledExecutorService    exec = Executors.newSingleThreadScheduledExecutor();
        final ScheduledExecutorService    exec2 = Executors.newSingleThreadScheduledExecutor();
        
//        ebus.add(exec);
//        ebus.add(exec2);
        
        exec.execute(new Runnable() {

//            @Override
            public void run() {
                ebus.registerObserver("demo.e1", new Demo(), "func2");
                
                synchronized (ebus) {
                    ebus.notifyAll();
                }
            }} );
        
        synchronized (ebus) {
            ebus.wait();
        }
        
        exec2.execute(new Runnable() {

//            @Override
            public void run() {
                Emitter emitter = null;
//                ebus.genUniqEmitter(new Demo(), "func1");
                
                ebus.fireEvent("demo.e1", emitter);
                
                synchronized (ebus) {
                    ebus.notifyAll();
                }
            }} );

        synchronized (ebus) {
            ebus.wait();
        }

//        unreg.run();
//        
//        ebus.fireEvent("/demo/print", "hello, world!");
//        ebus.fireEvent("/demo/printint", 89);
//        
        
        System.out.println("end in [" + Thread.currentThread().getId() + "].");
    }

}
